var app = getApp()
import ajaxhelper from '../../utils/ajaxhelper.js';
import { signByMd5 } from "../../utils/util";
var gloalAudio = require("../common/gloalAudio/gloalAudio.js");//全局音乐模块
Page({
  data: { 
    isPlayGloalAudio: false,//是否在播放全局音频
    currentTab: 0, //预设当前项的值
    dataList: [],//列表数据
    hasRefresh: false,
    isGetMore: false,
    hasMore: true,
    pageSize: 5,
    pageNo: 1,
    isShow: false,//是否显示没有列表的提示图
    subjectId:'',//类型id
  },
  onLoad:function(options){
    this.getListData();//获取列表数据
    this.getSubject();//分类
    // app.checkPlay(this);//需要显示全局音乐的时候显示   
  },
  getSubject(){
    let param1 = {
      "mediaType": "VIDEO",
      
    }
    ajaxhelper.post(app.globalData.frontJSHost + 'subject/KNOWLEDGE_VIDEO', param1, this, function (res) {
      console.log(res);
      if (res.result) {
        this.setData({
          subjectList:res.data
        })
      } else {
        wx.showToast({
          title: !!res.message.global ? res.message.global : "请求出错",
          icon: "loading"
        })
      }
    })
  },
  // 列表数据渲染
  getListData: function () {
    let param = {
      "mediaType":"VIDEO",
      "subjectId": this.data.subjectId
    }
    
    ajaxhelper.post(app.globalData.frontJSHost + 'knowledge/queryPage/' + this.data.pageNo + '/' + this.data.pageSize, param, this, this.setListData)
  },
  setListData(res) {
    if (res.result) {
      var pageNo = this.data.pageNo;
      var Data=res.data;
      var hasmore = Data.totalCount - pageNo * this.data.pageSize;
      this.setData({
        pageNo: ++pageNo,
        hasRefresh: false,
        isGetMore: false,
        hasMore: hasmore
        // tip: '上拉加载更多数据'
      })
      if (Data && Data.result) {
        this.setData({
          dataList: this.data.dataList.concat(Data.result),
        })
      }
    } else {
      this.setData({
        hasRefresh: false,
        isGetMore: false,
      })
      wx.showToast({
        title: !!res.message.global ? res.message.global : "请求出错",
        icon: "loading"
      })
    }
    this.setData({
      isShow: true
    })

  },
  //下拉刷新
  onPullDownRefresh() {
    this.setData({
      isShow: false
    })
    if (this.data.hasRefresh) return false;
    this.setData({
      dataList: [],
      hasRefresh: true,
      pageNo: 1
    })
    this.getListData()
    setTimeout(function () {
      wx.stopPullDownRefresh()
    }, 1400);

  },
  //上拉加载
  onReachBottom() {
    if (this.data.isGetMore) return false;
    if (this.data.hasRefresh) return false;
    if (this.data.hasMore > 0) {
      this.setData({
        isGetMore: true,
        // tip: '正在加载中...'
      })
      this.getListData();
    } else {
      this.setData({
        isGetMore: false,
        // tip: '没有更多数据了...'
      })
      return false
    }
  },
  // 点击标题切换当前页时改变样式
  swichNav: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var cur = e.currentTarget.dataset.current;
    if (this.data.currentTab == cur) { return false; }
    else {
      this.setData({
        currentTab: cur,
        subjectId:id,
        dataList: [],
        pageNo: 1
      })
    }
    this.getListData();//获取列表数据
  },
  // 视频专辑详情
  goVideoAlbumDetail(e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../videoAlbumDetail/videoAlbumDetail?id='+id,
    })
  },
  // 全局音乐弹框
  showGloalAudio() {
    gloalAudio.set('', {
      confirm: function (data) {
        console.log(data)
      }.bind(this)
    });
    gloalAudio.open('')
  },

})
